package bjtu.cit.bookshop.controller.admin;

import bjtu.cit.bookshop.common.ErrorCode;
import bjtu.cit.bookshop.common.Md5;
import bjtu.cit.bookshop.controller.BaseController;
import bjtu.cit.bookshop.entities.Book;
import bjtu.cit.bookshop.entities.Employee;
import bjtu.cit.bookshop.entities.Publisher;
import bjtu.cit.bookshop.entities.User;
import bjtu.cit.bookshop.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by jia19 on 2015/9/28.
 */
@Controller
@RequestMapping("/admin/")
public class AdminController extends BaseController {

    @Resource
    IEmployeeService employeeService = null;

    @Resource
    IBookService bookService = null;

    @Resource
    IPublisherService publisherService = null;

    @Resource
    IOrderService orderService = null;

    @Resource
    IUserService userService = null;

    @Resource
    INoteService noteService = null;

    @ResponseBody
    @RequestMapping("doneLogin.do")
    public ErrorCode doneLogin(HttpServletRequest request, HttpSession session)
    {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if (username != null && password != null)
        {
            password = Md5.getMd5(password);
            Employee employee = employeeService.getEmployeeByUsername(username);
            if (employee != null && employee.getPassword().equals(password)){
                session.setAttribute("isLogin", "true");
                session.setAttribute("loginName", username);
                session.setAttribute("userId", employee.getId());
                session.setAttribute("admin", "true");
                return SUCCESS;
            }
            else {
                session.setAttribute("isLogin", "false");
                session.setAttribute("loginName", "");
                session.setAttribute("userId", "");
                return BAD_PASSWORD;
            }
        }
        else
        {
            return INFORMATION_MISSING;
        }
    }

    @RequestMapping("index.do")
    public String index(HttpSession session, ModelMap model) {
        String loginName = null;
        if (( loginName = checkAdminLogin(session) ) != null) {
            model.addAttribute("isLogin", true);
            model.addAttribute("loginName", loginName);
            return "/admin/alreadyLogin";
        }
        else {
            return "/admin/index";
        }
    }


}
